package com.hbhmzy.call;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hbhmzy.utils.Client;
import com.hbhmzy.utils.ConfigEntity;

import java.io.UnsupportedEncodingException;
import java.util.Timer;
import java.util.TimerTask;


public class WantCallFragment extends Fragment implements View.OnClickListener {

    private View view;
    public static String name,targetname;
    public static String symbol;
    public boolean life = true;

    private ImageView call;
    private EditText ep;
    private TextView my_tex;


    InputMethodManager inputManager;
    MainActivity.LocalReceiver_start localReceiver_start;
    MainActivity.LocalReceiver_off localReceiver_off;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = inflater.inflate(R.layout.fragment_want_call, container, false);
        initView();
//        rejectbroadcast();
        return view;
    }

    private void initView() {
        call = view.findViewById(R.id.call);
        ep = view.findViewById(R.id.edit_phone);
        my_tex = view.findViewById(R.id.my_phone);
        call.setOnClickListener(this);

        //获取本机号码，并赋值给name；
        Intent intent = getActivity().getIntent();
        name = intent.getStringExtra("name");
        my_tex.setText(name);


        //延时0.5秒弹出软键盘
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                inputManager = (InputMethodManager) ep.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.showSoftInput(ep, 0);
            }
        }, 500);

//
//        //登录成功后 UDP和TCP同时通知服务器 已上线
//        try {
//            new Client(ConfigEntity.serverIP, ConfigEntity.serverTCPPort, getActivity()).action();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
//        Client.send(name + "login");
    }
//    @Override
//    public void onViewCreated(View view, Bundle savedInstanceState) {
//        super.onViewCreated(view, savedInstanceState);
//        call = view.findViewById(R.id.call);
//        ep = view.findViewById(R.id.edit_phone);
//        my_tex = view.findViewById(R.id.my_phone);
//        call.setOnClickListener(this);
//
//        //获取本机号码，并赋值给name；
//        Intent intent = getActivity().getIntent();
//        name = intent.getStringExtra("name");
//        my_tex.setText(name);
//
//        //延时0.5秒弹出软键盘
//        Timer timer = new Timer();
//        timer.schedule(new TimerTask() {
//            public void run() {
//                inputManager = (InputMethodManager) ep.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
//                inputManager.showSoftInput(ep, 0);
//            }
//        }, 500);
//


//    private void rejectbroadcast() {
//        //注册本地广播接收器 接收"hbhmzy.start"类型广播
//        Log.e("tag", "rejectbroadcast: start");
//        IntentFilter intentFilter_start = new IntentFilter();
//        intentFilter_start.addAction("hbhmzy.start");
//        localReceiver_start = new MainActivity.LocalReceiver_start();
//        getActivity().getApplication().registerReceiver(localReceiver_start, intentFilter_start);
//
//
//        //注册本地广播接收器 接收"hbhmzy.offline"类型广播
//        Log.e("tag", "rejectbroadcast: offline");
//        IntentFilter intentFilter_off = new IntentFilter();
//        intentFilter_off.addAction("hbhmzy.offline");
//        localReceiver_off = new WantCallFragment.LocalReceiver_off();
//        getActivity().getApplication().registerReceiver(localReceiver_off, intentFilter_off);
//    }

    private void showKeyboard() {
        inputManager = (InputMethodManager) ep.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.showSoftInput(ep, 0);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.call: {
                targetname = ep.getText().toString();
                if (targetname.equals("")) {
                    Toast.makeText(getActivity(), "请您先输入拨打的用户电话", Toast.LENGTH_SHORT).show();
                } else if (targetname.length() < 11) {
                    Toast.makeText(getActivity(), "请输入有效的电话号码", Toast.LENGTH_SHORT).show();
                } else {
                    symbol = name + "#" + targetname + ":";// 1#2:  或者 2#1:标记
                    Intent intent1 = new Intent(getActivity(), CallingActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putBoolean("is_online", false);
                    bundle.putString("symbol", symbol);
                    intent1.putExtras(bundle);
                    startActivity(intent1);
                    Client.send(symbol + "start");
                }
                break;
            }
            case R.id.main_tab_want_call_img: {
                showKeyboard();
            }
            break;
            default:
                break;
        }
    }

    //当有电话打过来的时候 跳转到等待接听页面
    class LocalReceiver_start extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Intent intent1 = new Intent(getActivity(), WaitCallActivity.class);
            Bundle bundle = new Bundle();
            bundle.putBoolean("is_online", false);
            bundle.putString("symbol", symbol);
            intent1.putExtras(bundle);
            startActivity(intent1);
        }
    }


    //当点击电话，对方不在线时。
    class LocalReceiver_off extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(getActivity(), "您拨打的用户暂时不在线，请稍后再拨", Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    public void onDestroy() {
        if (localReceiver_start != null)
            getActivity().getApplicationContext().unregisterReceiver(localReceiver_start);
        if (localReceiver_off != null)
            getActivity().getApplicationContext().unregisterReceiver(localReceiver_off);
        super.onDestroy();
        Client.send(name + "logout");
    }
}
